package com.vrview.administrator.fresco;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.PskKeyManager;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;

/**
 * Created by Administrator on 2016/9/12.
 */
public class ImageAdapter extends BaseAdapter implements AbsListView.OnScrollListener {

    /**
     * 上下文对象的引用
     */
    private Context context;
    /**
     * Image Url的数组
     */
    private String[] imageUrls;
    /**
     * ListView对象的引用
     */
    private ListView mListView;
    /**
     * 记录是否刚打开程序，用于解决进入程序不滚动屏幕，不会下载图片的问题。
     * 参考http://blog.csdn.net/guolin_blog/article/details/9526203#comments
     */
    private boolean isFirstEnter = true;
    /**
     * 一屏中第一个item的位置
     */
    private int mFirstVisibleItem;

    /**
     * 一屏中所有item的个数
     */
    private int mVisibleItemCount;

    public ImageAdapter(Context context, ListView mListView, String[] imageUrls) {
        this.context = context;
        this.mListView = mListView;
        this.imageUrls = imageUrls;
        mListView.setOnScrollListener(this);
    }

    @Override
    public int getCount() {
        return imageUrls.length;
    }

    @Override
    public Object getItem(int position) {
        return imageUrls[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final String mImageUrl = imageUrls[position];
        ImageHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_main, parent, false);
            holder = new ImageHolder();
            holder.draweeView = (SimpleDraweeView) convertView.findViewById(R.id.user_avator);
            convertView.setTag(holder);
        } else {
            holder = (ImageHolder) convertView.getTag();
        }

        //给draweeView设置一个Tag，防止错位
        holder.draweeView.setTag(mImageUrl);
        //holder.draweeView.setImageURI(Uri.parse((String) holder.draweeView.getTag())); //最简单的使用，复杂的使用controller

        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(Uri.parse((String) holder.draweeView.getTag()))
                .setTapToRetryEnabled(true)
                .setOldController(holder.draweeView.getController())
                .build();
        holder.draweeView.setController(controller);

        return convertView;
    }

    //暂停图片请求
    public static void imagePause() {

        Fresco.getImagePipeline().pause();
    }

    //恢复图片请求
    public static void imageResume() {

        Fresco.getImagePipeline().resume();
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

        switch (scrollState){
            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE://空闲状态
                imageResume();
                break;
            case AbsListView.OnScrollListener.SCROLL_STATE_FLING://滚动状态
                imagePause();
                break;
            case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://触摸后滚动
                imagePause();
                break;
        }

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }

    static class ImageHolder {

        SimpleDraweeView draweeView;
    }

}
